package websocket.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import websocket.dto.ResultDTO;
import websocket.entity.vo.SerialPortVo;
import websocket.exception_.*;
import websocket.listener.MyListener;
import websocket.service.ConnectionManageService;
import websocket.util.ComBeanUtil;
import websocket.util.SerialTool;

import java.util.ArrayList;


@Service
public class ConnectionManageServiceImpl implements ConnectionManageService {

    private static final Logger log = LoggerFactory.getLogger(ConnectionManageServiceImpl.class);
    //航向/最佳位置名称
    @Value("${chaunKou.hangXiangName}")
    private String hangXiangName;
    //测量点名称
    @Value("${chaunKou.horizontalName}")
    private String horizontalName;
    //无人机闭合通路名称
    @Value("${chaunKou.wuRenJiName}")
    private String wuRenJiName;
    //波特率
    @Value("${chaunKou.boTeLv}")
    private Integer boTeLv;

    @Override
    public ResultDTO getSerialPortList() {
        ArrayList<String> ports=new ArrayList<>();
        try {
            ports = SerialTool.findPort();
        }catch (Exception e){
            log.info(e.getMessage());
        }
        return ResultDTO.success(ports);
    }

    @Override
    public ResultDTO connectSerialPort(String wrjPort, String hxPort, String cldPort) {
        try {
            if (StringUtils.isNotBlank(hxPort)) {
                if (ComBeanUtil.getMap(hxPort) == null) {
                    log.info("【航向】打开串口：" + hxPort);
                    log.info("【发送指令】：unlogall");
                    log.info("【发送指令】：log headingb ontime 1");
                    log.info("【发送指令】：SAVECONFIG");
                    SerialPortVo serialPortVo = SerialTool.openPort(hangXiangName, hxPort, boTeLv);
                    String sendChuanKouData0 = "unlogall";
                    serialPortVo.sendData(sendChuanKouData0.getBytes());
                    String sendChuanKouData1 = "log headingb ontime 1";
                    serialPortVo.sendData(sendChuanKouData1.getBytes());
                    String sendChuanKouData2 = "SAVECONFIG";
                    serialPortVo.sendData(sendChuanKouData2.getBytes());
                    serialPortVo.bindListener(new MyListener(), 500);
                }
            } else {
                log.info("未发现【航向串口】" + hxPort);
                return ResultDTO.externalError(503, "未发现【航向串口】");
            }
            if (StringUtils.isNotBlank(cldPort)) {
                if (ComBeanUtil.getMap(cldPort) == null) {
                    log.info("【水平仪】打开串口：" + cldPort);
                    log.info("【发送指令】：unlog all");
                    log.info("【发送指令】：log bestposb ontime 1");
                    log.info("【发送指令】：SAVECONFIG");
                    SerialPortVo shuiPingYiVO = SerialTool.openPort(horizontalName, cldPort, boTeLv);
                    String sendZhiLing0 = "unlogall";
                    shuiPingYiVO.sendData(sendZhiLing0.getBytes());
                    String sendZhiLing1 = "log bestposb ontime 1";
                    shuiPingYiVO.sendData(sendZhiLing1.getBytes());
                    String sendZhiLing2 = "SAVECONFIG";
                    shuiPingYiVO.sendData(sendZhiLing2.getBytes());
                    shuiPingYiVO.bindListener(new MyListener(), 500);
                }
            } else {
                log.info("未发现【测量点串口】" + cldPort);
                return ResultDTO.externalError(503, "未发现【测量点串口】");
            }

            if (StringUtils.isNotBlank(wrjPort)) {
                if (ComBeanUtil.getMap(wrjPort) == null) {
                    log.info("【无人机闭合通路】打开串口：" + wrjPort);
                    log.info("【发送指令】：unlogall");
                    log.info("【发送指令】：log bestposb ontime 1");
                    log.info("【发送指令】：SAVECONFIG");
                    SerialPortVo shuiPingYiVO = SerialTool.openPort(wuRenJiName, wrjPort, boTeLv);
                    String sendZhiLing0 = "unlog all";
                    shuiPingYiVO.sendData(sendZhiLing0.getBytes());
                    String sendZhiLing1 = "log bestposb ontime 1";
                    shuiPingYiVO.sendData(sendZhiLing1.getBytes());
                    String sendZhiLing2 = "SAVECONFIG";
                    shuiPingYiVO.sendData(sendZhiLing2.getBytes());
                    shuiPingYiVO.bindListener(new MyListener(), 500);
                }
            } else {
                log.info("未发现【无人机闭合通路串口】" + wrjPort);
                return ResultDTO.externalError(503, "未发现【无人机闭合通路串口】");
            }

        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (PortInUse portInUse) {
            portInUse.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (NoSuchPort noSuchPort) {
            noSuchPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (NotASerialPort notASerialPort) {
            notASerialPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (SerialPortParameterFailure serialPortParameterFailure) {
            serialPortParameterFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (TooManyListeners tooManyListeners) {
            tooManyListeners.printStackTrace();
            return ResultDTO.externalError(503, "监听数量过多！添加操作失败！");
        }
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO connectPortWrj(String wrjPort) {
        try {

            if (StringUtils.isNotBlank(wrjPort)) {
                if (ComBeanUtil.getMap(wrjPort) == null) {
                    log.info("【无人机闭合通路】打开串口：" + wrjPort);
                    log.info("【发送指令】：unlogall");
                    log.info("【发送指令】：log com3 bestposb ontime 1");
                    log.info("【发送指令】：SAVECONFIG");
                    SerialPortVo shuiPingYiVO = SerialTool.openPort(wuRenJiName, wrjPort, boTeLv);
                    String sendZhiLing0 = "unlog all";
                    shuiPingYiVO.sendData(sendZhiLing0.getBytes());
                    String sendZhiLing1 = "log com3 bestposb ontime 1";
                    shuiPingYiVO.sendData(sendZhiLing1.getBytes());
                    String sendZhiLing2 = "SAVECONFIG";
                    shuiPingYiVO.sendData(sendZhiLing2.getBytes());

                    String sendChuanKouData4 = "\\n";
                    shuiPingYiVO.sendData(sendChuanKouData4.getBytes());
                    shuiPingYiVO.bindListener(new MyListener(), 500);
                }
            } else {
                log.info("未发现【无人机闭合通路串口】" + wrjPort);
                return ResultDTO.externalError(503, "未发现【无人机闭合通路串口】");
            }

        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (PortInUse portInUse) {
            portInUse.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (NoSuchPort noSuchPort) {
            noSuchPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (NotASerialPort notASerialPort) {
            notASerialPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (SerialPortParameterFailure serialPortParameterFailure) {
            serialPortParameterFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (TooManyListeners tooManyListeners) {
            tooManyListeners.printStackTrace();
            return ResultDTO.externalError(503, "监听数量过多！添加操作失败！");
        }
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO connectPorthx(String hxPort) {
        try {
            if (StringUtils.isNotBlank(hxPort)) {
                if (ComBeanUtil.getMap(hxPort) == null) {
                    log.info("【航向】打开串口：" + hxPort);
                    log.info("【发送指令】：unlogall");
                    log.info("【发送指令】：log com3 bestposb ontime 1");
                    log.info("【发送指令】：log com3 headingb ontime 1");
                    log.info("【发送指令】：SAVECONFIG");
                    SerialPortVo serialPortVo = SerialTool.openPort(hangXiangName, hxPort, boTeLv);
                    String sendChuanKouData0 = "unlogall";
                    serialPortVo.sendData(sendChuanKouData0.getBytes());
//                    String sendChuanKouData1 = "log headingb ontime 1";
                    String sendChuanKouData1 = "log com3 bestposb ontime 1";
                    serialPortVo.sendData(sendChuanKouData1.getBytes());

                    String sendChuanKouData3 = "log com3 headingb ontime 1";
                    serialPortVo.sendData(sendChuanKouData3.getBytes());

                    String sendChuanKouData2 = "SAVECONFIG";
                    serialPortVo.sendData(sendChuanKouData2.getBytes());

                    String sendChuanKouData4 = "\\n";
                    serialPortVo.sendData(sendChuanKouData4.getBytes());
                    serialPortVo.bindListener(new MyListener(), 500);
                }
            } else {
                log.info("未发现【航向串口】" + hxPort);
                return ResultDTO.externalError(503, "未发现【航向串口】");
            }


        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (PortInUse portInUse) {
            portInUse.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (NoSuchPort noSuchPort) {
            noSuchPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (NotASerialPort notASerialPort) {
            notASerialPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (SerialPortParameterFailure serialPortParameterFailure) {
            serialPortParameterFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (TooManyListeners tooManyListeners) {
            tooManyListeners.printStackTrace();
            return ResultDTO.externalError(503, "监听数量过多！添加操作失败！");
        }
        return ResultDTO.success(null);
    }

    @Override
    public ResultDTO connectPortcld(String cldPort) {
        try {
            if (StringUtils.isNotBlank(cldPort)) {
                if (ComBeanUtil.getMap(cldPort) == null) {
                    log.info("【测量点】打开串口：" + cldPort);
                    log.info("【发送指令】：unlog all");
                    log.info("【发送指令】：log com3 bestposb ontime 1");
                    log.info("【发送指令】：SAVECONFIG");
                    SerialPortVo shuiPingYiVO = SerialTool.openPort(horizontalName, cldPort, boTeLv);
                    String sendZhiLing0 = "unlogall";
                    shuiPingYiVO.sendData(sendZhiLing0.getBytes());
//                    String sendZhiLing1 = "log bestposb ontime 1";
                    String sendZhiLing1 = "log com3 bestposb ontime 1";
                    shuiPingYiVO.sendData(sendZhiLing1.getBytes());
                    String sendZhiLing2 = "SAVECONFIG";
                    shuiPingYiVO.sendData(sendZhiLing2.getBytes());

                    String sendZhiLing3= "\\n";
                    shuiPingYiVO.sendData(sendZhiLing3.getBytes());
                    shuiPingYiVO.bindListener(new MyListener(), 500);
                }
            } else {
                log.info("未发现【测量点串口】" + cldPort);
                return ResultDTO.externalError(503, "未发现【测量点串口】");
            }



        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (PortInUse portInUse) {
            portInUse.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败");
        } catch (NoSuchPort noSuchPort) {
            noSuchPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (NotASerialPort notASerialPort) {
            notASerialPort.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (SerialPortParameterFailure serialPortParameterFailure) {
            serialPortParameterFailure.printStackTrace();
            return ResultDTO.externalError(503, "串口连接失败！");
        } catch (TooManyListeners tooManyListeners) {
            tooManyListeners.printStackTrace();
            return ResultDTO.externalError(503, "监听数量过多！添加操作失败！");
        }
        return ResultDTO.success(null);
    }


}
